package com.iyundao.io.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iyundao.io.Service.IntegralService;
import com.iyundao.io.mapper.*;
import com.iyundao.io.model.*;
import com.iyundao.io.tools.ResponseData;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class IntegralServiceImpl implements IntegralService {

    @Autowired
    private IntegralMapper integralMapper;

    @Autowired
    private IntegralRecordMapper integralRecordMapper;

    @Autowired
    private ActivityCategoryMapper activityCategoryMapper;

    @Autowired
    private ActivityModelMapper activityModelMapper;

    @Autowired
    private ActivityModelCustomMapper modelCustomMapper;

    @Autowired
    private PartyMemberCustomMapper memberCustomMapper;

    /**
     * 积分变更
     * @param integralRecord
     *          活动id不为空，礼品id为空时，进行积分增加操作
     *          活动id为空，礼品id，操作人id不为空时执行减分操作
     * @return
     */
    @Override
    public ResponseData updateIntegralSum(IntegralRecord integralRecord) {
        if (integralRecord.equals("") || integralRecord == null) {
            return new ResponseData(0, "传入数据为空");
        } else {

            /**
             * 根据数据判断增减积分
             * activityid不为空giftid为空时，积分增加
             * activityid为空giftid不为空时，积分下降
             */

            if (integralRecord.getActivityid() != null && integralRecord.getActivityid() != 0) {
                Integral integral = integralMapper.selectByPartyMemberId(integralRecord.getPartymemberid());

                //活动每次签到应得积分
                Integer i = modelCustomMapper.selectById(integralRecord.getActivityid()).getActivityintegral();
                //Integer i = activityCategoryMapper.selectById(modelCustomMapper.selectById(integralRecord.getActivityid()).getActivityid()).getCategoryintegral();

                /**
                 * 判断是否第一次获得积分，是执行添加操作，否执行修改操作
                 */
                if (integral == null || integral.equals("")) {
                    Integral integral1 = new Integral();
                    integral1.setIntegralsum(i);
                    integral1.setIntegralnow(i);
                    integral1.setPartymemberid(integralRecord.getPartymemberid());
                    int i1 = integralMapper.insertSelective(integral1);
                    if (i1 > 0) {
                        integralRecord.setIntegralrecordnum(i);
                        return addIntegralRecord(integralRecord);
                    } else {
                        return new ResponseData(0, "积分添加失败");
                    }
                } else {
                    integral.setIntegralsum(integral.getIntegralsum() + i);
                    integral.setIntegralnow(integral.getIntegralnow() + i);
                    int i1 = integralMapper.update(integral);
                    if (i1 > 0) {
                        integralRecord.setIntegralrecordnum(i);
                        return addIntegralRecord(integralRecord);
                    } else {
                        return new ResponseData(0, "积分添加失败");
                    }
                }
            } else {
                /**
                 * 扣除积分的操作
                 */
                Integral integral = integralMapper.selectByPartyMemberId(integralRecord.getPartymemberid());
                integral.setIntegralnow(integral.getIntegralnow() - integralRecord.getIntegralrecordnum());
                int i = integralMapper.update(integral);
                if (i > 0) {
                    return addIntegralRecord(integralRecord);
                } else {
                    return new ResponseData(0, "积分兑换失败");
                }
            }
        }

    }

    /**
     * 积分记录操作
     */
    @Override
    public ResponseData addIntegralRecord(IntegralRecord integralRecord) {
        if (integralRecord == null || integralRecord.equals("")) {
            return new ResponseData(0, "传入参数为空");
        } else {
            Date date = new Date();
            integralRecord.setIntegralrecordtime(date);
            int i = integralRecordMapper.insertSelective(integralRecord);
            if (i > 0) {
                return new ResponseData(200, "记录添加成功");
            } else {
                return new ResponseData(0, "记录添加失败");
            }
        }

    }


    /**
     * 查询个人总积分和现有积分
     * @param partymemberid
     *      用户id
     * @return
     */
    @Override
    public ResponseData selectIntegralByPartyMemberId(Integer partymemberid) {
        try {
            Integral integral = integralMapper.selectByPartyMemberId(partymemberid);
            return new ResponseData(200, "成功", integral, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData(0, e.getMessage());
        }
    }

    /**
     * @param currentPage
     *      第几页
     * @param pageSize
     *      每页显示数据条数
     * @return
     *      个人积分变更记录
     */
    @Override
    public PageInfo<IntegralRecordShow> showIntegralRecord(int currentPage, int pageSize) {
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        PartyMember partyMember = memberCustomMapper.selectAllByPhone(sysUser.getPhone());
        List<IntegralRecordShow> list = integralRecordMapper.selectByPartyMemberId(partyMember.getId());
        PageHelper.startPage(currentPage, pageSize);
        PageInfo<IntegralRecordShow> pageInfo = new PageInfo<>(list);
        return  pageInfo;
    }

    /**
     * 查询兑换记录
     * @param pagenum
     * @param pagesize
     * @return
     */
    @Override
    public PageInfo<IntegralRecordShow> selectExchange(int pagenum, int pagesize) {
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        PartyMember partyMember = memberCustomMapper.selectAllByPhone(sysUser.getPhone());
        List<IntegralRecordShow> list = integralRecordMapper.selectExchange(partyMember.getId());
        PageHelper.startPage(pagenum,pagesize);
        PageInfo<IntegralRecordShow> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public ResponseData selectRecordBySid(int pagenum, int pagesize, String sid,String date, String date1) {
        PageHelper.startPage(pagenum,pagesize);
        List<IntegralShow> list = integralRecordMapper.selectRecordBySid(sid,date,date1);
        PageInfo<IntegralShow> pageInfo = new PageInfo<>(list);
        return new ResponseData(200,"成功",pageInfo);
    }
}
